package com.haoyi.feignapi.mapper;

import com.haoyi.feignapi.entity.Attachment;
import com.haoyi.feignapi.entity.Order;
import com.haoyi.feignapi.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface EnMallMapper {

    List<Map<String, Object>> getOrder(Map<String, Object> paramMap);

    @Select("SELECT a.*,b.part_name FROM t_order_attachment a LEFT JOIN t_attachment b ON a.part_code = b.part_code WHERE a.order_code = #{order_code} AND b.language = #{language}")
    List<Map<String, Object>> getOrderParts1(Map<String, Object> paramMap);

    @Select("SELECT * " +
            "FROM t_order " +
            "WHERE " +
            "status = #{v2} " +
            "AND (creation_time BETWEEN #{v3} AND #{v4}) " +
            "AND (code LIKE #{v5} OR firm_name LIKE #{v5} OR phone LIKE #{v5} OR recipients LIKE #{v5})")
    List<Order> getOrderEN(String v2, String v3, String v4, String v5);

    @Insert("INSERT INTO t_user(email,firm_name,permission_id,name,country,erp_code,bs_user_id,bs_user_email) " +
            "VALUES ( #{email},#{firm_name},2,#{name},#{country},#{erp_code},#{bs_user_id},#{bs_user_email})")
    void signinOverseasUser(User user);

    @Select("SELECT *  FROM t_user WHERE permission_id = #{permission_id} AND (erp_code LIKE #{search} OR email LIKE #{search} OR phone LIKE #{search} OR firm_name LIKE #{search} OR username LIKE #{search})")
    List<User> getUserBySearch(Integer permission_id, String search);

    @Select("SELECT a.*,b.remark AS pric_namech FROM t_user a LEFT JOIN t_price_discount b ON a.price_discount_id = b.id WHERE a.permission_id=#{permission_id}")
    List<User> getUserByPermission(Integer permission_id);

    List<Attachment> getAttachment(@RequestParam("machine_id") Integer machine_id, @RequestParam("type") String type, @RequestParam("search") String search);


    List<Map<String, Object>> getOrderParts(Map<String, Object> paramMap);


    Integer processingCheckBsUserId(Map<String, Object> paramMap);

    @Update("UPDATE t_order SET status=#{status},${statusDate}=CURRENT_TIMESTAMP WHERE code = #{code}")
    void setOrderStatus(String code, Integer status, String statusDate);

    List<Map<String, Object>> getUserData(Map<String, Object> paramMap);

    @Select(" SELECT COUNT(*) FROM t_order" +
            " where  creation_time >= #{date1} AND creation_time <= #{date2}")
    Integer getAllTradeCount(Date date1, Date date2);
}
